<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="工号">
            <el-input v-model="userInfo.workNumber" class="inputW" />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              style="width: 300px"
              type="date"
              class="inputW"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 姓名 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="userInfo.username" class="inputW" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input v-model="userInfo.departmentName" class="inputW" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--手机 聘用形式  -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" style="width: 300px" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="聘用形式">
            <el-select v-model="userInfo.formOfEmployment" class="inputW">
              <el-option
                v-for="item in EmployeeEnum.hireType"
                :key="item.id"
                :label="item.value"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- 组件 -->
            <upload-img ref="imgUrl" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="saveUser">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>

        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { getUserDetailIdAPI } from '@/api/user'
import EmployeeEnum from '@/constant/employees'
import { saveUserDetailByIdAPI } from '@/api/employees'

export default {
  name: 'UserInfo',
  data() {
    return {
      userId: this.$route.query.id,
      EmployeeEnum, // 员工枚举数据
      userInfo: {}
    }
  },
  created() {
    // 调用获取用户信息的 API
    this.getUserDetailIdFn()
  },
  methods: {
    // 获取员工个人信息
    async getUserDetailIdFn() {
      const res = await getUserDetailIdAPI(this.userId)
      // const res = await getUserDetailIdAPI(this.this.$route.query.id)

      this.userInfo = res.data

      // 将后端返回的图片地址复制给子组件中的 imageUrl 中--渲染页面
      this.$refs.imgUrl.imageUrl = res.data.staffPhoto
    },

    // 保存员工个人信息的更改--点击更新-- --ref
    async saveUser() {
      // 获取到子组件中的 实例 imgUrl 起的名字--获取到图片
      const avatar = this.$refs.imgUrl.imageUrl
      // 将图片赋值给 userInfo 的字段
      this.userInfo.staffPhoto = avatar
      const res = await saveUserDetailByIdAPI(this.userInfo)
      this.$message.success(res.message)
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
